package com.xaicode.template.rabbitmq.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Locker
 * @date 2021/8/20 0020 17:22
 */
@Slf4j
@Component
public class RabbitReceiver {

    @RabbitListener(queues = {"FANOUT_QUEUE_A"})
    public void fanoutA(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        log.info("FANOUT_QUEUE_A: {}", new String(message.getBody()));
    }

    @RabbitListener(queues = {"FANOUT_QUEUE_B"})
    public void fanoutB(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        log.info("FANOUT_QUEUE_B: {}", new String(message.getBody()));
    }

    @RabbitListener(queues = {"DIRECT_QUEUE"})
    public void direct(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        log.info("DIRECT_QUEUE: {}", new String(message.getBody()));
    }

}
